package com.sgb.member.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.member.PickCateType;
import com.sgb.member.R;
import com.sgb.member.databinding.ActivityClientDynamicStateBinding;
import com.sgb.member.di.DaggerMemberComponent;
import com.sgb.member.di.MemberModule;
import com.sgb.member.entity.ClientDynamicStateBean;
import com.sgb.member.ui.adapter.ClientDynamicStateAdapter;
import com.sgb.member.view_model.ClientDynamicStateModel;
import com.swgk.core.base.AppActivity;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.MToast;
import com.swgk.core.view.areapopup.AddressEntity;
import com.swgk.core.view.areapopup.GoodsSourceLocationEntity;
import com.swgk.core.view.areapopup.ThreeLevelAreaWindow;
import com.swgk.core.view.selection.CommonPopEntity;
import com.swgk.core.view.selection.MemberStatusView;
import com.swgk.core.xrecyclerview.LoadingMoreFooter;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * Created by：冯齐 on 2021/1/26 16:42
 * Description：会员动态
 */
public class ClientDynamicStateActivity extends AppActivity implements MemberStatusView.OnChooseListener {

    @Inject
    ClientDynamicStateModel mModel;
    private ActivityClientDynamicStateBinding mBinding;
    private String mNo1;     //行业分类
    private String mNo2;     //动态类型
    private String mNo4;     //地区省市
    private String mNo5;     //地区省市
    private CommonPopEntity mAll;

    // 经营地址Popup
    private ThreeLevelAreaWindow mAddressPop;
    private List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> scrData;

    private ClientDynamicStateAdapter mAdapter;

    public static void start(Context context) {
        Intent intent = new Intent(context, ClientDynamicStateActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerMemberComponent.builder()
                .appComponent(appComponent)
                .memberModule(new MemberModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initView() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_client_dynamic_state);
        initSearchView();
        initRecycler();
        mAll = new CommonPopEntity();
        mAll.setNo("");
        mAll.setTypeName("全部");
        mBinding.popup1.initPopup(mBinding.grayLayout, mBinding.popLine, this);
        mBinding.popup2.initPopup(mBinding.grayLayout, mBinding.popLine, this);
        mBinding.popupAddress.initPopup(mBinding.grayLayout, mBinding.popLine, this);
        mBinding.popupAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (scrData == null) {
                    mModel.getAddressModel(true);
                } else {
                    showAddressPopup();
                }
            }
        });
        initAddressPopup();
        loadData(false);
    }

    //请求固定资产列表数据信息
    public void loadData(boolean isLoadMore) {
        mModel.getMemberCompanyNews(isLoadMore, mSearchText, mNo1, mNo2, mNo4, mNo5);
    }

    private void initAddressPopup() {
        mAddressPop = new ThreeLevelAreaWindow(getActivity());
        mAddressPop.setBgView(mBinding.grayLayout);
        mAddressPop.setMemberHeight();
        mAddressPop.setCitySingle();
        mAddressPop.setProvinceSingle();
        mAddressPop.setShowSimpleName(true);
        mAddressPop.setOnlyShowProAndCity(true);
        mAddressPop.setAllSingleSelect(true);
        mAddressPop.setThreeListIsGone(true);
        mAddressPop.setOnResultClick(new ThreeLevelAreaWindow.OnResultClickProxy() {
            @Override
            protected void result(GoodsSourceLocationEntity entity) {
                mNo4 = "";
                mNo5 = "";
                mBinding.popupAddress.updateChooseTextStatus(0, mBinding.popupAddress.getKeyName());
                if (!TextUtils.isEmpty(entity.getProvinceNo())) {
                    mNo4 = entity.getProvinceNo();
                    mBinding.popupAddress.updateChooseTextStatus(1, entity.getProvinceName());
                    if (!TextUtils.isEmpty(entity.getCityNo())) {
                        mNo5 = entity.getCityNo();
                        mBinding.popupAddress.updateChooseTextStatus(1, entity.getCityName());
                    }
                }
                //刷新数据
                loadData(false);
            }
        });
        mAddressPop.setOnDismissListener(() -> {
            mBinding.popLine.setVisibility(View.GONE);
            if (!TextUtils.isEmpty(mNo4) || !TextUtils.isEmpty(mNo5)) {
                mBinding.popupAddress.updateChooseTextStatus(1, null);
            } else {
                mBinding.popupAddress.updateChooseTextStatus(0, mBinding.popupAddress.getKeyName());
            }
        });
    }

    private void showAddressPopup() {
        mBinding.popLine.setVisibility(View.VISIBLE);
        if (!TextUtils.isEmpty(mNo4) || !TextUtils.isEmpty(mNo5)) {
            mBinding.popupAddress.updateChooseTextStatus(-2, null);
        } else {
            mBinding.popupAddress.updateChooseTextStatus(-1, mBinding.popupAddress.getKeyName());
        }
        mAddressPop.showPopwindow0(mBinding.popLine);
    }

    /**
     * --------------------------------- 搜索相关 -----------------------------------------------
     */
    private String mSearchText = ""; // 搜索内容

    private void initSearchView() {
        mBinding.activityBar.ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mBinding.activityBar.etView.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        mBinding.activityBar.etView.setHint("搜索企业名称、动态标题");
        mBinding.activityBar.etView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (TextUtils.isEmpty(mBinding.activityBar.etView.getText().toString())) {
                        MToast.showToast(getActivity(), "请输入企业名称、动态标题");
                        return true;
                    }
                    if (mSearchText.equals(mBinding.activityBar.etView.getText().toString())) {
                        MToast.showToast(getActivity(), "请输入不同的搜索内容");
                        return true;
                    }
                    mSearchText = mBinding.activityBar.etView.getText().toString().trim();
                    loadData(false);
                }
                return false;
            }
        });
        mBinding.activityBar.etView.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (TextUtils.isEmpty(s) || s.length() == 0) {
                    mBinding.activityBar.tvSearchDelete.setVisibility(View.INVISIBLE);
                } else {
                    mBinding.activityBar.tvSearchDelete.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mBinding.activityBar.tvSearchDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSearchText = "";
                mBinding.activityBar.etView.setText("");
                loadData(false);
            }
        });
        mBinding.activityBar.tvSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(mSearchText)) {
                    if (TextUtils.isEmpty(mBinding.activityBar.etView.getText().toString())) {
                        MToast.showToast(getActivity(), "请输入企业名称");
                        return;
                    }
                }
                if (mSearchText.equals(mBinding.activityBar.etView.getText().toString())) {
                    MToast.showToast(getActivity(), "请输入不同的搜索内容");
                    return;
                }
                mSearchText = mBinding.activityBar.etView.getText().toString().trim();
                loadData(false);
            }
        });
        mBinding.activityBar.tvSearchDelete.setVisibility(View.INVISIBLE);
    }

    /**
     * --------------------------------- recyclerView相关 -------------------------------
     */

    private void initRecycler() {
        TextView textView = mBinding.emptyView.findViewById(R.id.no_data_count);
        textView.setVisibility(View.VISIBLE);
        mBinding.recycler.setPullRefreshEnabled(true);
        mBinding.recycler.setLoadingMoreEnabled(true);
        LoadingMoreFooter footer = (LoadingMoreFooter) mBinding.recycler.getFootView();
        footer.setNoMoreHint("已经到底啦~");
        mBinding.recycler.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new ClientDynamicStateAdapter(this, null);
        mBinding.recycler.setAdapter(mAdapter);
        mBinding.recycler.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                loadData(false);
            }

            @Override
            public void onLoadMore() {
                loadData(true);
            }
        });
        mAdapter.setOnItemClick(new AdapterOnItemClick<ClientDynamicStateBean>() {
            @Override
            public void onItemClick(ClientDynamicStateBean bean, int position) {
                // 点击整个动态模块，跳转到对应的动态详情
                mModel.getUrlSetting("dynamic", bean.getId());
            }
        });
    }

    public void showEmpty() {
        mBinding.recycler.refreshComplete();
        mBinding.recycler.loadMoreComplete();
        mBinding.recycler.setVisibility(View.GONE);
        mBinding.emptyView.setVisibility(View.VISIBLE);
    }

    public void refreshRecycler(List<ClientDynamicStateBean> list, boolean isLoadMore, boolean isLastPage) {
        mBinding.recycler.setVisibility(View.VISIBLE);
        if (isLoadMore) {
            mBinding.recycler.loadMoreComplete();
            if (list != null && list.size() > 0) {
                mAdapter.addDatas(list);
            }
            mBinding.recycler.setNoMore(isLastPage);
        } else {
            mBinding.recycler.refreshComplete();
            if (list != null && list.size() > 0) {
                mBinding.emptyView.setVisibility(View.GONE);
                mAdapter.setDatas(list);
            } else {
                mAdapter.clearDatas();
                mBinding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void onLoadChooseData(String mKeyName) {
        switch (mKeyName) {
            case "行业分类":
                mModel.getDynamicCategoryList(true);
                break;
            case "动态类型":
                mModel.getDynamicList(true);
                break;
        }
    }

    @Override
    public void onChooseResult(String mKeyName, CommonPopEntity entity, Object... objects) {
        int position = (int) objects[0];
        String no = position == 0 ? "" : entity.getNo();
        switch (mKeyName) {
            case "行业分类":
                if (!TextUtils.equals(no, mNo1)) {
                    mNo1 = no;
                    loadData(false);
                }
                break;
            case "动态类型":
                if (!TextUtils.equals(no, mNo2)) {
                    mNo2 = no;
                    loadData(false);
                }
                break;
        }
    }

    public void setAddressData(boolean isShowPopup, List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> data, List<CommonPopEntity> entities) {
        if (data != null && data.size() > 0) {
            scrData = data;
            entities.add(0, mAll);
            if (TextUtils.isEmpty(mNo4)) {
                mAddressPop.setData(data, true);
            } else {
                mAddressPop.setDataAndPosition(data, mNo4, mNo5, "", true, true);
            }
            if (isShowPopup) {
                showAddressPopup();
            }
        }
    }

    // 企业动态-行业分类 no
    public void setDynamicCategoryList(boolean isShowPopup, List<CommonPopEntity> data) {
        if (data == null || data.size() == 0) return;
        data.add(0, mAll);
        mBinding.popup1.setData(data);
        if (isShowPopup) {
            mBinding.popup1.showPop();
        }
    }

    // 企业动态-动态类型
    public void setDynamicList(boolean isShowPopup, List<CommonPopEntity> data) {
        if (data == null || data.size() == 0) return;
        data.add(0, mAll);
        mBinding.popup2.setData(data);
        if (isShowPopup) {
            mBinding.popup2.showPop();
        }
    }

}
